package com.farm.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.farm.pojo.User;
import com.farm.service.UserService;
import com.farm.common.AppHttpCodeEnum;
import com.farm.common.ResponseResult;
import com.farm.util.AppJwtUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author farm
 * @since 2023-10-30
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

//    @Autowired
//    private KafkaTemplate<String,String> kafkaTemplate;
//
//    @Autowired
//    private RedisTemplate<Object,Object> template;
    @PostMapping("/user/login")
    public ResponseResult userLogin(@RequestBody User user){
        return userService.userLogin(user);
    }

    @PostMapping("/root/login")
    public ResponseResult rootLogin(@RequestBody User user) {
        System.out.println(user);
        if(StringUtils.isNotBlank(user.getUsername()) && StringUtils.isNotBlank(user.getPassword())){
            User dbUser = userService.getOne(Wrappers.<User>lambdaQuery().eq(User::getUsername, user.getUsername()).eq(User::getState,1).eq(User::getPower,4));
            if(dbUser == null){
                return ResponseResult.errorResult(AppHttpCodeEnum.AP_USER_DATA_NOT_EXIST,"用户信息不存在");
            }
            String salt = dbUser.getSalt();
            String password = user.getPassword();
            String pswd = DigestUtils.md5DigestAsHex((password + salt).getBytes());
            System.out.println(pswd);
            if(!pswd.equals(dbUser.getPassword())){
                return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR,"密码错误");
            }
            String token = AppJwtUtil.getToken(dbUser.getUserId());
            Map<String,Object> map =new HashMap<>();
            map.put("token",token);
            dbUser.setSalt("");
            dbUser.setPassword("");
            map.put("user",dbUser);
            return ResponseResult.okResult(map);
        }else {
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,"密码错误");
        }
    }
    @PostMapping("/user/info")
    public ResponseResult getUserInfo(@RequestBody User user){
        return userService.getUserInfo(user);
    }

    @GetMapping("/admin/getlist")
    public ResponseResult adminGetList(int current,int size){
        Page pageInfo = new Page(current,size);
        return ResponseResult.okResult(userService.page(pageInfo));
    }

    @GetMapping("/admin/getlist2")
    public ResponseResult adminGetList2(){
        return ResponseResult.okResult(userService.list(
                Wrappers.<User>lambdaQuery().eq(User::getState,1)
        ));
    }

    @PostMapping("/admin/givepower")
    public ResponseResult givePower(@RequestBody User user){
        userService.updateById(user);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @PostMapping("/admin/edit")
    public ResponseResult adminEdit(@RequestBody User user){
        String salt = "abcd";
        String password = user.getPassword();
        String pswd = DigestUtils.md5DigestAsHex((password + salt).getBytes());
        user.setPassword(pswd);
        userService.updateById(user);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @PostMapping("/admin/update")
    public ResponseResult updateUserStatus(@RequestBody User user){
        userService.updateById(user);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @PostMapping("/user/changePass")
    public ResponseResult changePass(@RequestBody User user){
        String salt = "abcd";
        String pswd = DigestUtils.md5DigestAsHex((user.getPassword() + salt).getBytes());
        user.setPassword(pswd);
        user.setSalt(salt);
        System.out.println(user);
        userService.updateById(user);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @PostMapping("/admin/changeState")
    public ResponseResult changeState(@RequestBody User user){
        System.out.println("开始了");
        System.out.println(user);
//        template.opsForValue().set(user.getUserId() + "", JSON.toJSONString(user));
//        kafkaTemplate.send("farm.user",user.getUserId().toString());
        userService.updateById(user);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @PostMapping("/user/register")
    public ResponseResult userRegister(@RequestBody User user){
        System.out.println(user);
        if(StringUtils.isNotBlank(user.getUsername()) && StringUtils.isNotBlank(user.getPassword())){
            User dbUser = userService.getOne(Wrappers.<User>lambdaQuery().eq(User::getUsername, user.getUsername()));
            if(dbUser != null){
                return ResponseResult.errorResult(AppHttpCodeEnum.AP_USER_DATA_NOT_EXIST,"用户存在");
            }
            String salt = "abcd";
            String password = user.getPassword();
            String pswd = DigestUtils.md5DigestAsHex((password + salt).getBytes());
            user.setPassword(pswd);
            System.out.println(pswd);
            userService.save(user);
        }
        return ResponseResult.okResult("ok");
    }



}

